home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / ABGR.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.7 KB  |  218 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40.  
  41. /* abgr.c - Demonstrates the use of the extension EXT_abgr.
  42.  
  43.    The same image data is used for both ABGR and RGBA formats
  44.    in glDrawPixels and glTexImage2D.  The left side uses ABGR,
  45.    the right side RGBA.  The top polygon demonstrates use of texture,
  46.    and the bottom image is drawn with glDrawPixels.
  47.  
  48.    Note that the textures are defined as 3 component, so the alpha
  49.    value is not used in applying the DECAL environment.  */
  50.  
  51. #include <string.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <GL/glut.h>
  55.  
  56. GLenum doubleBuffer;
  57. GLubyte ubImage[65536];
  58.  
  59. static void
  60. Init(void)
  61. {
  62.   int j;
  63.   GLubyte *img;
  64.   GLsizei imgWidth = 128;
  65.  
  66.   glMatrixMode(GL_PROJECTION);
  67.   glLoadIdentity();
  68.   gluPerspective(60.0, 1.0, 0.1, 1000.0);
  69.   glMatrixMode(GL_MODELVIEW);
  70.   glDisable(GL_DITHER);
  71.  
  72.   /* Create image */
  73.   img = ubImage;
  74.   for (j = 0; j < 32 * imgWidth; j++) {
  75.     *img++ = 0xff;
  76.     *img++ = 0x00;
  77.     *img++ = 0x00;
  78.     *img++ = 0xff;
  79.   }
  80.   for (j = 0; j < 32 * imgWidth; j++) {
  81.     *img++ = 0xff;
  82.     *img++ = 0x00;
  83.     *img++ = 0xff;
  84.     *img++ = 0x00;
  85.   }
  86.   for (j = 0; j < 32 * imgWidth; j++) {
  87.     *img++ = 0xff;
  88.     *img++ = 0xff;
  89.     *img++ = 0x00;
  90.     *img++ = 0x00;
  91.   }
  92.   for (j = 0; j < 32 * imgWidth; j++) {
  93.     *img++ = 0x00;
  94.     *img++ = 0xff;
  95.     *img++ = 0x00;
  96.     *img++ = 0xff;
  97.   }
  98. }
  99.  
  100. /* ARGSUSED1 */
  101. static void
  102. Key(unsigned char key, int x, int y)
  103. {
  104.   switch (key) {
  105.   case 27:
  106.     exit(0);
  107.   }
  108. }
  109.  
  110. void
  111. TexFunc(void)
  112. {
  113.   glEnable(GL_TEXTURE_2D);
  114.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  115.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  116.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  117.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  118.  
  119. #if GL_EXT_abgr
  120.   glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_ABGR_EXT,
  121.     GL_UNSIGNED_BYTE, ubImage);
  122.  
  123.   glBegin(GL_POLYGON);
  124.   glTexCoord2f(1.0, 1.0);
  125.   glVertex3f(-0.2, 0.8, -100.0);
  126.   glTexCoord2f(0.0, 1.0);
  127.   glVertex3f(-0.8, 0.8, -2.0);
  128.   glTexCoord2f(0.0, 0.0);
  129.   glVertex3f(-0.8, 0.2, -2.0);
  130.   glTexCoord2f(1.0, 0.0);
  131.   glVertex3f(-0.2, 0.2, -100.0);
  132.   glEnd();
  133. #endif
  134.  
  135.   glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGBA,
  136.     GL_UNSIGNED_BYTE, ubImage);
  137.  
  138.   glBegin(GL_POLYGON);
  139.   glTexCoord2f(1.0, 1.0);
  140.   glVertex3f(0.8, 0.8, -2.0);
  141.   glTexCoord2f(0.0, 1.0);
  142.   glVertex3f(0.2, 0.8, -100.0);
  143.   glTexCoord2f(0.0, 0.0);
  144.   glVertex3f(0.2, 0.2, -100.0);
  145.   glTexCoord2f(1.0, 0.0);
  146.   glVertex3f(0.8, 0.2, -2.0);
  147.   glEnd();
  148.  
  149.   glDisable(GL_TEXTURE_2D);
  150. }
  151.  
  152. static void
  153. Draw(void)
  154. {
  155.  
  156.   glClearColor(0.0, 0.0, 0.0, 1.0);
  157.   glClear(GL_COLOR_BUFFER_BIT);
  158.  
  159. #if GL_EXT_abgr
  160.   glRasterPos3f(-0.8, -0.8, -1.5);
  161.   glDrawPixels(128, 128, GL_ABGR_EXT, GL_UNSIGNED_BYTE, ubImage);
  162. #endif
  163.  
  164.   glRasterPos3f(0.2, -0.8, -1.5);
  165.   glDrawPixels(128, 128, GL_RGBA, GL_UNSIGNED_BYTE, ubImage);
  166.  
  167.   TexFunc();
  168.  
  169.   if (doubleBuffer) {
  170.     glutSwapBuffers();
  171.   } else {
  172.     glFlush();
  173.   }
  174. }
  175.  
  176. static void
  177. Args(int argc, char **argv)
  178. {
  179.   GLint i;
  180.  
  181.   doubleBuffer = GL_TRUE;
  182.  
  183.   for (i = 1; i < argc; i++) {
  184.     if (strcmp(argv[i], "-sb") == 0) {
  185.       doubleBuffer = GL_FALSE;
  186.     } else if (strcmp(argv[i], "-db") == 0) {
  187.       doubleBuffer = GL_TRUE;
  188.     }
  189.   }
  190. }
  191.  
  192. int
  193. main(int argc, char **argv)
  194. {
  195.   GLenum type;
  196.  
  197.   glutInit(&argc, argv);
  198.   Args(argc, argv);
  199.  
  200.   type = GLUT_RGB;
  201.   type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  202.   glutInitDisplayMode(type);
  203.   glutCreateWindow("ABGR extension");
  204.   if (!glutExtensionSupported("GL_EXT_abgr")) {
  205.     printf("Couldn't find abgr extension.\n");
  206.     exit(0);
  207.   }
  208. #if !GL_EXT_abgr
  209.   printf("WARNING: client-side OpenGL has no ABGR extension support!\n");
  210.   printf("         Drawing only RGBA (and not ABGR) images and textures.\n");
  211. #endif
  212.   Init();
  213.   glutKeyboardFunc(Key);
  214.   glutDisplayFunc(Draw);
  215.   glutMainLoop();
  216.   return 0;             /* ANSI C requires main to return int. */
  217. }
  218.